package com.eascs.workflow.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.backFees.entity.O2oBackFeesEntity;
import com.eascs.admin.o2o.backFees.service.O2oBackFeesService;
import com.eascs.admin.o2o.backfee.entity.FormBackFeeEntity;
import com.eascs.admin.o2o.backfee.service.O2OComBankFeeService;
import com.eascs.admin.o2o.cacle.entity.O2oCacleFlowEntity;
import com.eascs.admin.o2o.cacle.service.O2oCacleFlowServiceImpl;
import com.eascs.admin.o2o.credit.entity.CreditReport;
import com.eascs.admin.o2o.credit.service.CreditReportService;
import com.eascs.admin.o2o.detail.service.LoanDetailService;
import com.eascs.admin.o2o.enterpriseZX.entity.O2oEnterpriseZxEntity;
import com.eascs.admin.o2o.enterpriseZX.service.O2oEnterpriseZxService;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.entity.LoanapplyExpandDetail;
import com.eascs.admin.o2o.internal.service.AuditCommentService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandDetailService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.loanAccount.entity.O2oCollectFee;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oLoadUserZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.service.O2oRecommendService;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.brand.service.O2oBrandService;
import com.eascs.brand.vo.BrandAndPartnerVo;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.file.FileTypeConstants;
import com.eascs.loan.entity.O2oBankLoanData;
import com.eascs.loan.service.O2oLoanService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.service.dictionary.DicDataService;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.form.WorkFlowForm;

@Controller
public class ProcessTaskRestController extends BaseRestController {
	
	private static Logger logger = LoggerFactory.getLogger(ProcessTaskRestController.class);
	
	@Reference
	private WorkFlowService workFlowService;
	@Reference
	private SysPermissionService sysPermissionService;
	@Reference
	private DicDataService dicDataService;
	@Autowired
	private AuditCommentService auditCommentService;
	@Autowired
	private BaseUtils baseUtils;
	@Reference
	private ICustomerInfoService iCustomerInfoService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Value("${o2o.admin.iframe.domain}")
	private String domain;
	@Autowired
	O2OComBankFeeService o2oComBankFeeService;
	@Autowired
	CreditReportService creditReportService;
	@Autowired
	private O2oLoanService loanService;
	@Autowired
	private LoanapplyExpandDetailService loanapplyExpandDetailService;
	@Autowired
	private LoanDetailService loanDetailService;
	@Autowired
	private O2oBrandService o2oBrandService;
	
	@Reference
	SysOrgService service;
	
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	
	@Autowired
	O2oCacleFlowServiceImpl o2oCacleFlowServiceImpl;
	
	@Autowired
	O2oLoadUserZxServiceImpl o2oLoadUserZxServiceImpl;
	
	@Autowired
	O2oEnterpriseZxService o2oEnterpriseZxService;
	
	@Autowired
	O2oBackFeesService o2oBackFeesService;
	
	@Autowired
	IO2oCollectFeesService o2oCollectFeesService;
	
	@Autowired
	O2oRecommendService o2oRecommendService;
	
	
	@Reference
	CustomerService customerService;
	
	@Value("${web.homeUrl}")
	String serviceHomeUrl;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "approvalManage");
	}

	/**
	 * 
	 * /** 代办审批列表 详情页面
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval", method = RequestMethod.GET)
	public HtmlRestResult approval(@RequestParam("loanapplyNo") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		CreditReport creditReport = creditReportService.queryLoanapplyNo(loanapplyNo, String.valueOf(StatusContexts.O2O_ZERO));
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		BrandAndPartnerVo brand=o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
		ViewContext.put("brand", brand);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		String isNotContinue=null;
		O2oLoanapplyEntity loanapplyEntity=loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(),loanapply.getCreateDate());
		if(loanapplyEntity!=null){
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue="Y";
		}
		ViewContext.put("isNotContinue", isNotContinue);
		ViewContext.put("loanapply", loanapply);
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		
		// =================借款人/配偶征信报告 begin=================
		String userZxId="";
		String mateZxId="";
		if (loanUser != null) {
			userZxId = loanUser.getZxNo();
		}
		if (materEntity != null) {
			mateZxId = materEntity.getZxNo();
		}
		
		ViewContext.put("userZxId", userZxId);
		ViewContext.put("mateZxId", mateZxId);
		// =================借款人/配偶征信报告 end=================
		
		// =================征信附件 begin=================
		// 征信附件 屏蔽下载:true
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		// =================征信附件 end=================
		
		ViewContext.put("creditReport", creditReport);
		ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(loanapplyNo));
		PageResult<Object[]> auditResult = auditCommentService.findAuditList(loanapplyNo);
		String auditTitles[] = { "EA批复额度", "综合费率", "送审银行", "贷款用途", "审批期限", "审批还款方式", "风险补偿金利率", "审批保证金费率", "审批管理费费率", "审批贷款利率", "审批人", "审批时间", "备注" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("BANKTYPEO2O"), new SelectDataOption("PAYMENTTYPE"), new SelectDataOption("OTOLOANTERM"), new SelectDataOption("OTOREPAYMENTWAY"), null, new SelectDataOption("O2ORISKDEPOSITFEERATE"),
				new SelectDataOption("O2ODEPOSITFEERATE"), new SelectDataOption("O2OMANAGEFEERATE"), null, null, null, null };
		PaginationInfo auditInfo = new PaginationInfo(auditResult, auditTitles);
		auditInfo.setSelectDataOptions(selectDataOptions);
		auditInfo.setBuildPagingNavigation(false);
		auditInfo.setFirstRenderColumnIndex(0);
		auditInfo.setBuildRowNumber(true);
		auditInfo.setFormats(new Format[] { new DecimalFormat(",##0.00"), null, null, null, null, null, null, null, null, null, null, null });
		ViewContext.put("auditResult", auditInfo);
		ViewContext.put("domain", domain);
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityByloanapply(loanapplyNo);
		ViewContext.put("O2oEnterpriseZxEntity", o2oEnterpriseZxEntity);
		return HtmlRestResult.popHtmlFragment("approval/approval");
	}
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 作废
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/newcacle", method = RequestMethod.GET)
	public HtmlRestResult approvalNewCacle(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		
		O2oCacleFlowEntity o2oCacleFlowEntity = o2oCacleFlowServiceImpl.getEntityById(loanapplyNo);
		
		O2oLoanapplyEntity loan = o2oLoanapplyService.findById(o2oCacleFlowEntity.getLoanapplyNo());
		
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loan.getLoanId(), loan.getLoanapplyNo());
		
		ViewContext.put("o2oCacleFlowEntity", o2oCacleFlowEntity);
		ViewContext.put("o2oLoanUserEntityZx", loanUser);
		
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		return HtmlRestResult.popHtmlFragment("approval/approval_cacle");
	}
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/newzx", method = RequestMethod.GET)
	public HtmlRestResult approvalNewZX(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		
		O2oZxEntity o2oZxEntity  = o2oZxServiceImpl.getEntityById(loanapplyNo);
		
		O2oLoanUserEntityZx o2oLoanUserEntityZx = o2oLoadUserZxServiceImpl.getEntityByZxNo(loanapplyNo);
		
		//Button button = ButtonHelper.createAjaxButton("启动流程", "/work/newstartup?loanapplyNo=" + o2oZxEntity.getId());
		//button.setPermissionCode("NewZxStartFlowButton");
		//ViewContext.put("button", button);
		
		// =================征信附件 begin=================
		// 征信附件,屏蔽下载:true
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		// =================征信附件 end=================
		
		ViewContext.put("o2oZxEntity", o2oZxEntity);
		ViewContext.put("o2oLoanUserEntityZx", o2oLoanUserEntityZx);
//		o2oZxServiceImpl.queryDatalist(o2oZxEntity.getId());
		o2oZxServiceImpl.queryDatalistIsReadOnly(o2oZxEntity.getId(),datalistReadonly);
		
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		
		return HtmlRestResult.popHtmlFragment("approval/approval_newzx");
	}
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/newRecommend", method = RequestMethod.GET)
	public HtmlRestResult approvalnewRecommend(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		
		O2oRecommendEntity o2oRecommendEntity =o2oRecommendService.getById(loanapplyNo);
		
		ViewContext.put("o2oRecommendEntity", o2oRecommendEntity);
		List<ActOrgEntity> orgProvince = service.getOrgProvinceAndHQ();
		ViewContext.put("ActOrgEntitys", orgProvince);
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		return HtmlRestResult.popHtmlFragment("approval/approval_newRecommend");
	}
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/newTX", method = RequestMethod.GET)
	public HtmlRestResult approvalnewTX(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		
		
		O2oBackFeesEntity  o2oBackFeesEntity = o2oBackFeesService.queryById(loanapplyNo);
		
		ViewContext.put("o2oBackFees", o2oBackFeesEntity);
		
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		StringBuffer o2oPageUrl = new StringBuffer("/rbackfee/o2obackfee");
		String o2oUrl = "";
		
		CustomerEntity loaner = customerService.queryCustomerEntityByIdCard(o2oBackFeesEntity.getDocumentNo());
		CustomerEntity spouse = loaner.getSpouse();
		
		if(spouse!=null){
			
			logger.info("配偶信息:", spouse.getIdNumber() +"-------"+ spouse.getCustomerName());
			
		}
		
		FormBackFeeEntity formBackFee = new FormBackFeeEntity();
		if (formBackFee != null) {
			String customerId = loaner.getCustomerId() == null ? "" : loaner.getCustomerId();
			String spouseId ="";
			if(spouse!=null){
				 spouseId = spouse.getCustomerId() == null ? "" : spouse.getCustomerId();
			}
			o2oUrl = UrlUtil.appendUrl(o2oPageUrl.toString(), "customerId", customerId);
			o2oUrl = UrlUtil.appendUrl(o2oUrl.toString(), "spouseId", spouseId);
		}
		o2oUrl = o2oUrl + "&showO2oPage=";
		
		ViewContext.put("o2oBackfeeUrl", o2oUrl);
		
		if(loaner!=null){
			ViewContext.put("customerIdCard", loaner.getIdNumber());
			ViewContext.put("accountId", loaner.getAccountId());
		}else{
			ViewContext.put("customerIdCard", null);
			ViewContext.put("accountId", null);
		}
		
		if(spouse!=null){
			ViewContext.put("spouseIdCard", spouse.getIdNumber());
		}else{
			ViewContext.put("spouseIdCard", null);
		}
		
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		return HtmlRestResult.popHtmlFragment("approval/approval_newTX");
	}
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/newQYzx", method = RequestMethod.GET)
	public HtmlRestResult approvalnewQYzx(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityById(loanapplyNo);
		
		ViewContext.put("o2oEnterprise", o2oEnterpriseZxEntity);
		
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		// =================征信附件 begin=================
		// 征信附件 屏蔽下载:true
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		// =================征信附件 end=================
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		return HtmlRestResult.popHtmlFragment("approval/approval_newQYzx");
	}
	
	
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/zx", method = RequestMethod.GET)
	public HtmlRestResult approvalZX(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		CreditReport creditReport = creditReportService.queryLoanapplyNo(loanapplyNo, String.valueOf(StatusContexts.O2O_ZERO));
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		BrandAndPartnerVo brand=o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
		ViewContext.put("brand", brand);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		String isNotContinue=null;
		O2oLoanapplyEntity loanapplyEntity=loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(),loanapply.getCreateDate());
		if(loanapplyEntity!=null){
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue="Y";
		}
		ViewContext.put("isNotContinue", isNotContinue);
		ViewContext.put("loanapply", loanapply);
		
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		
		// =================借款人/配偶征信报告 begin=================
		String userZxId="";
		String mateZxId="";
		if (loanUser != null) {
			userZxId = loanUser.getZxNo();
		}
		if (materEntity != null) {
			mateZxId = materEntity.getZxNo();
		}
		
		ViewContext.put("userZxId", userZxId);
		ViewContext.put("mateZxId", mateZxId);
		// =================借款人/配偶征信报告 end=================
		
		// =================征信附件 begin=================
		// 征信附件 屏蔽下载:true
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		// =================征信附件 end=================
		
		ViewContext.put("creditReport", creditReport);
		ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(loanapplyNo));
		PageResult<Object[]> auditResult = auditCommentService.findAuditList(loanapplyNo);
		String auditTitles[] = { "EA批复额度", "综合费率", "送审银行", "贷款用途", "审批期限", "审批还款方式", "风险补偿金利率", "审批保证金费率", "审批管理费费率", "审批贷款利率", "审批人", "审批时间", "备注" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("BANKTYPEO2O"), new SelectDataOption("PAYMENTTYPE"), new SelectDataOption("OTOLOANTERM"), new SelectDataOption("OTOREPAYMENTWAY"), null, new SelectDataOption("O2ORISKDEPOSITFEERATE"),
				new SelectDataOption("O2ODEPOSITFEERATE"), new SelectDataOption("O2OMANAGEFEERATE"), null, null, null, null };
		PaginationInfo auditInfo = new PaginationInfo(auditResult, auditTitles);
		auditInfo.setSelectDataOptions(selectDataOptions);
		auditInfo.setBuildPagingNavigation(false);
		auditInfo.setFirstRenderColumnIndex(0);
		auditInfo.setBuildRowNumber(true);
		auditInfo.setFormats(new Format[] { new DecimalFormat(",##0.00"), null, null, null, null, null, null, null, null, null, null, null });
		ViewContext.put("auditResult", auditInfo);
		ViewContext.put("domain", domain);
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityByloanapply(loanapplyNo);
		ViewContext.put("O2oEnterpriseZxEntity", o2oEnterpriseZxEntity);
		return HtmlRestResult.popHtmlFragment("approval/approval_zx");
	}
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 内审
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/ns", method = RequestMethod.GET)
	public HtmlRestResult approvalNS(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		CreditReport creditReport = creditReportService.queryLoanapplyNo(loanapplyNo, String.valueOf(StatusContexts.O2O_ZERO));
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		BrandAndPartnerVo brand=o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
		ViewContext.put("brand", brand);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		String isNotContinue=null;
		O2oLoanapplyEntity loanapplyEntity=loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(),loanapply.getCreateDate());
		if(loanapplyEntity!=null){
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue="Y";
		}
		ViewContext.put("isNotContinue", isNotContinue);
		ViewContext.put("loanapply", loanapply);
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		
		// =================借款人/配偶征信报告 begin=================
		String userZxId = "";
		String mateZxId = "";
		if (loanUser != null) {
			userZxId = loanUser.getZxNo();
		}
		if (materEntity != null) {
			mateZxId = materEntity.getZxNo();
		}
		
		ViewContext.put("userZxId", userZxId);
		ViewContext.put("mateZxId", mateZxId);
		// =================借款人/配偶征信报告 end=================
		
		// =================征信附件 begin=================
		// 征信附件 屏蔽下载:true
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		// =================征信附件 end=================
		
		ViewContext.put("creditReport", creditReport);
		ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(loanapplyNo));
		PageResult<Object[]> auditResult = auditCommentService.findAuditList(loanapplyNo);
		String auditTitles[] = { "EA批复额度", "综合费率", "送审银行", "贷款用途", "审批期限", "审批还款方式", "风险补偿金利率", "审批保证金费率", "审批管理费费率", "审批贷款利率", "审批人", "审批时间", "备注" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("BANKTYPEO2O"), new SelectDataOption("PAYMENTTYPE"), new SelectDataOption("OTOLOANTERM"), new SelectDataOption("OTOREPAYMENTWAY"), null, new SelectDataOption("O2ORISKDEPOSITFEERATE"),
				new SelectDataOption("O2ODEPOSITFEERATE"), new SelectDataOption("O2OMANAGEFEERATE"), null, null, null, null };
		PaginationInfo auditInfo = new PaginationInfo(auditResult, auditTitles);
		auditInfo.setSelectDataOptions(selectDataOptions);
		auditInfo.setBuildPagingNavigation(false);
		auditInfo.setFirstRenderColumnIndex(0);
		auditInfo.setBuildRowNumber(true);
		auditInfo.setFormats(new Format[] { new DecimalFormat(",##0.00"), null, null, null, null, null, null, null, null, null, null, null });
		ViewContext.put("auditResult", auditInfo);
		ViewContext.put("domain", domain);
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityByloanapply(loanapplyNo);
		
		ViewContext.put("O2oEnterpriseZxEntity", o2oEnterpriseZxEntity);
		return HtmlRestResult.popHtmlFragment("approval/approval_ns");
	}
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 征信
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/sbzj", method = RequestMethod.GET)
	public HtmlRestResult approvalSBZJ(WorkFlowForm workFlowForm) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(workFlowForm.getReferId());
		O2oBankLoanData bankData = loanService.queryBillCode(loanapply.getBillCode());
		LoanapplyExpand expand = loanapplyExpandService.queryLoanapplyNo(workFlowForm.getReferId());
		LoanapplyExpandDetail loanapplyExpandDetail = loanapplyExpandDetailService.queryDetail(expand.getId());
		if (loanapplyExpandDetail != null) {
			expand.setPaidDeposite(loanapplyExpandDetail.getPaidDeposite());
			expand.setPaidManger(loanapplyExpandDetail.getPaidManger());
			expand.setPaidRisk(loanapplyExpandDetail.getPaidRisk());
			expand.setPayDate(loanapplyExpandDetail.getPayDate());
			expand.setPayName(loanapplyExpandDetail.getPayName());
			expand.setPayRemark(loanapplyExpandDetail.getPayRemark());
			expand.setSubmitDate(loanapplyExpandDetail.getSubmitDate());
			expand.setTransferDeposit(loanapplyExpandDetail.getTransferDeposit());
			expand.setTransferNum(loanapplyExpandDetail.getTransferNum());
			expand.setReceiveDeposit(loanapplyExpandDetail.getReceiveDeposit());
		}
		if (expand.getPaidDeposite() == null) {
			expand.setPaidDeposite(expand.getReceiveDeposit());
		}
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		String isNotContinue=null;
		O2oLoanapplyEntity loanapplyEntity=loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(),loanapply.getCreateDate());
		if(loanapplyEntity!=null){
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue="Y";
		}
		ViewContext.put("isNotContinue", isNotContinue);
		BrandAndPartnerVo brand=o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
		ViewContext.put("brand", brand);
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("expand", expand);
		ViewContext.put("bankData", bankData);
		ViewContext.put("domain", domain);
		queryDataList(loanapply.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_BZJDATA, null);
		/*if(workFlowForm.getApprovalStatus().equals("FINISH") || workFlowForm.getApprovalStatus().equals("PROCESSING")){
			String agreeUrl = baseUtils.getApprovalAgreeUrl(workFlowForm.getReferId(), null, ProcessTemplate.SYSTEM_MODULE_O2O);
			ViewContext.put("agreeUrl", agreeUrl);
			return HtmlRestResult.popHtmlFragment("approval/deposit_detail");
		}else{
			String approvalUrl = baseUtils.getApprovalUrl(workFlowForm.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);
			ViewContext.put("approvalUrl", approvalUrl);
			return HtmlRestResult.popHtmlFragment("approval/deposit_approval");
		}*/
		String agreeUrl = baseUtils.getApprovalAgreeUrl(workFlowForm.getReferId(), null, ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		String approvalUrl = baseUtils.getApprovalUrl(workFlowForm.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", workFlowForm.getApprovalStatus());
		return HtmlRestResult.popHtmlFragment("approval/approval_sbzj");
	}
	
	/**
	 * 收取保证金和管理费 审批详情页面
	 * 
	 * @param referId
	 *            贷款ID
	 * @return
	 */
	@RequestMapping(value = "wf/depositview", method = RequestMethod.GET)
	public HtmlRestResult queryDepositTask(WorkFlowForm workFlowForm) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findById(workFlowForm.getReferId());
		O2oBankLoanData bankData = loanService.queryBillCode(loanapply.getBillCode());
		LoanapplyExpand expand = loanapplyExpandService.queryLoanapplyNo(workFlowForm.getReferId());
		LoanapplyExpandDetail loanapplyExpandDetail = loanapplyExpandDetailService.queryDetail(expand.getId());
		if (loanapplyExpandDetail != null) {
			expand.setPaidDeposite(loanapplyExpandDetail.getPaidDeposite());
			expand.setPaidManger(loanapplyExpandDetail.getPaidManger());
			expand.setPaidRisk(loanapplyExpandDetail.getPaidRisk());
			expand.setPayDate(loanapplyExpandDetail.getPayDate());
			expand.setPayName(loanapplyExpandDetail.getPayName());
			expand.setPayRemark(loanapplyExpandDetail.getPayRemark());
			expand.setSubmitDate(loanapplyExpandDetail.getSubmitDate());
			expand.setTransferDeposit(loanapplyExpandDetail.getTransferDeposit());
			expand.setTransferNum(loanapplyExpandDetail.getTransferNum());
			expand.setReceiveDeposit(loanapplyExpandDetail.getReceiveDeposit());
		}
		if (expand.getPaidDeposite() == null) {
			expand.setPaidDeposite(expand.getReceiveDeposit());
		}
		O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(), loanapply.getLoanapplyNo());
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("materEntity", materEntity);
		String isNotContinue=null;
		O2oLoanapplyEntity loanapplyEntity=loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(),loanapply.getCreateDate());
		if(loanapplyEntity!=null){
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue="Y";
		}
		ViewContext.put("isNotContinue", isNotContinue);
		BrandAndPartnerVo brand=o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
		ViewContext.put("brand", brand);
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("expand", expand);
		ViewContext.put("bankData", bankData);
		ViewContext.put("domain", domain);
		queryDataList(loanapply.getLoanapplyNo(), FileTypeConstants.DALIST_TYPE_BZJDATA, null);
		if(workFlowForm.getApprovalStatus().equals("FINISH") || workFlowForm.getApprovalStatus().equals("PROCESSING")){
			String agreeUrl = baseUtils.getApprovalAgreeUrl(workFlowForm.getReferId(), null, ProcessTemplate.SYSTEM_MODULE_O2O);
			ViewContext.put("agreeUrl", agreeUrl);
			return HtmlRestResult.popHtmlFragment("approval/deposit_detail");
		}else{
			String approvalUrl = baseUtils.getApprovalUrl(workFlowForm.getNodeAuditorId(), ProcessTemplate.SYSTEM_MODULE_O2O);
			ViewContext.put("approvalUrl", approvalUrl);
			return HtmlRestResult.popHtmlFragment("approval/deposit_approval");
		}
		
	}
	
	
	/**
	 * 
	 * /** 代办审批列表 详情页面 财务审核
	 * 
	 * @param loanapplyNo
	 *            贷款ID（必填）
	 * @param taskId
	 *            工作流ID（必填）
	 * @return
	 */
	@RequestMapping(value = "/wf/approval/cwsh", method = RequestMethod.GET)
	public HtmlRestResult approvalCwsh(@RequestParam("referId") String loanapplyNo, String nodeAuditorId, String approvalStatus) {
		O2oLoanapplyEntity loanapply = o2oLoanapplyService.findContinueById(loanapplyNo);
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapplyNo, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		ViewContext.put("loanapply", loanapply);

	
		ViewContext.put("domain", domain);
		return HtmlRestResult.popHtmlFragment("approval/approval_cwsh");
	}
	

	/**
	 * 上传凭证 附件详情
	 * 
	 * @param loanapplyNo
	 * @param code
	 * @param type
	 */
	public void queryDataList(String loanapplyNo, String code, String type) {
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanapplyNo, "'" + code + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("paginationInfo", paginationInfo);// 个人附件
	}
	
	
	/**
	 * 根据流程种类获取流程类型
	 * @param catagory
	 * @return
	 */
	@RequestMapping(value = "/wf/getProccessType", method = RequestMethod.GET)
	public BaseRestResult<List<DicData>> getProcessTypeByCatagory(@RequestParam("catagory") String catagory) {
		List<DicData> data = new ArrayList<DicData>();
		if (StringUtil.hasText(catagory)) {
			ModelResult<List<DicData>> modelResult = workFlowService.getProcessTypeByCatagory(catagory);
			data = modelResult.getModel();
		}
		return BaseRestResult.buildRestResult(data);
	}
	
	
	/**
	 * @Title: collectFees
	 * @Description: 个人账户收费
	 * @author 张庆奇
	 * @param id
	 * @param nodeAuditorId
	 * @param approvalStatus
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2017年12月5日 下午2:41:38
	 */
	@RequestMapping(value = "/wf/approval/collectFees", method = RequestMethod.GET)
	public HtmlRestResult collectFees(@RequestParam("referId") String id, String nodeAuditorId, String approvalStatus) {
		
		O2oCollectFee entity  = o2oCollectFeesService.queryById(id);
		
		
		String datalistReadonly = "true";
		if ("UNDO".equals(approvalStatus)) {
			datalistReadonly = "false";
		} else {
			datalistReadonly = "true";
		}
		ViewContext.put("datalistReadonly", datalistReadonly);
		
		
		ViewContext.put("entity", entity);
	
		//o2oZxServiceImpl.queryDatalistIsReadOnly(entity.getId(),datalistReadonly);
		
		String approvalUrl = baseUtils.getApprovalUrl(nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);// 审批代办列表详情URL
		approvalUrl += "&loadServiceUrl="+serviceHomeUrl+"/work/jqUpload";
		String agreeUrl = baseUtils.getApprovalAgreeUrl(id, nodeAuditorId, ProcessTemplate.SYSTEM_MODULE_O2O);
		
		ViewContext.put("agreeUrl", agreeUrl);
		ViewContext.put("approvalUrl", approvalUrl);
		ViewContext.put("approvalStatus", approvalStatus);// 流程经办状态是待办/已办/已发起，待办状态才显示审批栏
		
		
		return HtmlRestResult.popHtmlFragment("approval/approval_collectFees");
	}
}
